function extract_info() {
    $('div#info span.pl').each(function (idx, obj) {
        var text = $(obj).text();

        if (text.startsWith('作者')) {
            author = $(obj).next().text().strip();
        } else if (text.startsWith('出版社')) {
            publisher = $(obj.nextSibling).text().strip();
        } else if (text.startsWith('ISBN:')) {
            isbn = parseInt($(obj.nextSibling).text().strip());
        }
    });
    
    return {
        name: $('div#wrapper h1 span').text(),
        author: author,
        publisher: publisher,
        isbn: isbn
    };
}

function query_qingfanqie(info) {
    DOUBAN.update_button("qingfanqie", "查询", true).unbind('click').click(function () {
        chrome.extension.sendRequest({
            action: 'search',
            url: 'http://www.qingfanqie.com/book/booksearch.aspx?SearchType=1&bookName=' + info.name,
            info: info
        }, function (response) {

        });

        return false;
    });

    $('#loading').show();

    $.ajax({
        url: 'http://www.qingfanqie.com/book/booksearch.aspx',
        type: 'GET',
        data: {
            bookName: info.name,
            SearchType: 1
        },
        dataType: 'html',
        complete: function (xhr, status) {
            $('#loading').hide();
        },
        success: function (html) {
            var doc = $(html);

            var books = doc.find('.dtBorrowCart');

            if (books.length == 0)
            {
                DOUBAN.update_button("qingfanqie", "预借").unbind('click').click(function () {
                    chrome.extension.sendRequest({
                        action: 'order',
                        info: info
                    }, function (response) {

                    });
                    
                    return false;
                });
            }
            else
            {
                var book_id = $(books[0]).attr('onclick').toString().match(/AddBorrowCart\(\d+,(\d+)\)/)[1]

                DOUBAN.update_button("qingfanqie", "借阅").unbind('click').click(function() {
                    chrome.extension.sendRequest({
                        action: 'borrow',
                        info: info,
                        url: 'http://www.qingfanqie.com/Book/Book_Info.aspx?id=' + book_id
                    }, function (response) {

                    });

                    return false;
                });
            }
        },
        error: function (xhr, status, error) {
            console.log(status + error);
        }
    });
}

function query_nationallib(info) {
    var url = 'http://opac.nlc.gov.cn/F/?'+ $.param({
        func: 'find-m',
        request: info.name,
        find_code: 'WTP',
        find_code_1: 'WLN',
        find_code_2: 'WYR',
        find_code_4: 'WSL',
        filter_request_1: '',
        filter_request_2: '',
        filter_request_4: '',
        adjacent: 'Y',
        FIND_BASE: 'NLC01'
    });

    DOUBAN.update_button("nationallib", "国图").click(function () {
        console.log("open tab for the national lib " + url);

        chrome.extension.sendRequest({
            action: 'open',
            url: url
        }, function (response) {

        });

        return false;
    });
}

function show_buttons() {
    var info = extract_info();

    read_option('nationallib_button', function (key, value) {
        if (value) {
            query_nationallib(info);
        }
    });

    read_option('qingfanqie_button', function (key, value) {
        if (value) {
            query_qingfanqie(info);
        }
    });
}

show_buttons();
